package me.like.diandian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.LinearLayout;

import androidx.core.app.ActivityCompat;


import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.Disposable;
import me.like.diandian.R;
import me.like.diandian.api.Api;
import me.like.diandian.base.BaseActivity;
import me.like.diandian.base.BaseConstant;
import me.like.diandian.databinding.ActivityLoginBinding;
import me.like.diandian.util.AndroidBug5497Workaround;
import me.like.diandian.util.CommonUtils;
import me.like.diandian.util.RxUtils;
import me.like.diandian.util.SPUtils;
import me.like.diandian.view.ToastView;

import static me.like.diandian.util.CommonUtils.dp2px;

/**
 * Created by shousakai on 2019/6/20.
 */

public class LoginActivity extends BaseActivity<ActivityLoginBinding> {
    private int time = 60;
    private Disposable disposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidBug5497Workaround.assistActivity(this);
        onClick(binding.tvLogin, v -> {
            if (TextUtils.isEmpty(binding.etCode.getText()) || TextUtils.isEmpty(binding.etPhone.getText())) {
                ToastView.initToast().textToast(this, "手机号或密码为空");
            }
            if (!CommonUtils.matchPhone(binding.etPhone.getText() + "")) {
                ToastView.initToast().textToast(this, "手机号格式不正确");
                return;
            }
            Api.getInstance().messageLoginSubmit(BaseConstant.APPCHANNELID, binding.etPhone.getText() + "", binding.etCode.getText() + "", "REGIST_ANDROID")
                    .compose(RxUtils.applySchedulerAndAllFilter(LoginActivity.this)).subscribe(data -> {
                if (data.isSuccess()) {
                    SPUtils.setUserLogin(this, data.getData());
                    MainActivity.launch(this);
                    this.finish();
                }
            }, Throwable::printStackTrace);
        });
        onClick(binding.tvSendCode, v -> {
            if (TextUtils.isEmpty(binding.etPhone.getText())) {
                ToastView.initToast().textToast(this, "手机号为空");
                return;
            }
            if (!CommonUtils.matchPhone(binding.etPhone.getText() + "")) {
                ToastView.initToast().textToast(this, "手机号格式不正确");
                return;
            }
            Api.getInstance().sendPhone(binding.etPhone.getText() + "", "USER_REGIST")
                    .compose(RxUtils.applySchedulerAndAllFilter(LoginActivity.this)).subscribe(data -> {
                if (data.isSuccess()) {
                    countDown();
                }
            }, Throwable::printStackTrace);
        });
        if (getIntent().getBooleanExtra("canLeft", false)) {
            binding.tlvTitle.setLeftImg(R.mipmap.left_img);
        }
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) binding.tlvTitle.getLayoutParams();
        params.topMargin = CommonUtils.getStatusBarHeight(this) + dp2px(this, 5);
        binding.tlvTitle.setLayoutParams(params);
//        Api.getInstance().
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    private void countDown() {
        time = 60;
        binding.tvSendCode.setEnabled(false);
        binding.tvSendCode.setTextColor(ActivityCompat.getColor(this, R.color.c_757682));
        disposable = RxUtils.intervel(1, TimeUnit.SECONDS, (result, data) -> {
            time--;
            binding.tvSendCode.setText(time + "s");
            if (time == 0) {
                binding.tvSendCode.setTextColor(ActivityCompat.getColor(this, R.color.c_ff721f));
                binding.tvSendCode.setEnabled(true);
                disposable.dispose();
                binding.tvSendCode.setText("发送验证码");
            }

        });
    }

    public static void launch(Context context, boolean canLeft) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("canLeft", canLeft);
        context.startActivity(intent);
    }
}
